/*************************************************************************
*
*	science_main.c
*
*  Copyright 2007,2008 Steve Stancliff
*
*  Part of the TAOSF SSA system.
*
*
*  This module provides functions for simulating science telemetry,
*    i.e., environmental sensors.
*
*
**************************************************************************
*
*  version 1.3.6, 4/24/09, SBS
*    - documentation
*
*  version 1.3.5, 1/30/09, SBS
*   - add #ifdef SCIENCE_SIMULATE_FOO
*
*  version 1.3.4, 10/4/08, SBS
*   - documentation
*
*  version 1.3.3, 8/27/08, SBS
*    - add simulateScience_init()
*
*  version 1.3.2, 7/12/08, SBS
*    - add NEED_INSTNAMES to fix compile with debug on
*    - reduce log verbosity
*
*  version 1.3.1, 5/30/08, SBS
*    - move time format into status.h
*
*  version 1.3, 5/17/08, SBS
*    - move sensor-specific functions into separate files
*
*  version 1.2.5, 5/17/08, SBS
*    - integrate drift (rename current) into simulate_science
*    - change wind simulation to use constant velocity
*    - add wind and current angles to instrument types
*    - add writing units to SCI_DBL 
*    - minor cleanup
*
*  version 1.2.4, 5/12/08, SBS
*    - add simulate_drift()
*
*  version 1.2.3, 3/25/08, ELR
*    - add fluorometer gaussian
*
*  version 1.2.2, 3/4/08, SBS
*    - add randomized data for other data types
*    - add position and time setting in output data
*    - add fluorometer ellipsoid
*
*  version 1.2.1, 3/3/08, ELR
*    - add bounds checking to fluorometry queries
*
*  version 1.2, 2/29/08, ELR
*    - add simulated strips of fluorometry data
*
*  version 1.0.2, 2/26/08, SBS
*    - convert to cpp
*
*  version 1.0.1, 1/6/08, SBS
*    - add position input to simulate_science()
*    - move list of instruments to simulate_science.h
*
*  version 1.0, 12/17/07, SBS
*    - split from simulator.c
*
**************************************************************************
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful, but 
*  WITHOUT ANY WARRANTY; without even the implied warranty of 
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
*  General Public License for more details.
*
*  You should have received a copy of the GNU General Public License 
*  along with this program; if not, write to the Free Software Foundation, 
*  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
**************************************************************************/
/**
*  @defgroup plat_science
*	@ingroup platform
*  This module simulates properties of the world.
*/
/**
*  @file 
*  @ingroup plat_science
*  @brief
*  This module provides functions for simulating science telemetry,
*    i.e., environmental sensors.
*  <hr>
**/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#include "waypoints.h"
#include "platform_types.h"
#include "platform_modes.h"
#include "status.h"
#include "logfile.h"

#ifdef SCIENCE_SIMULATE_FLUOROMETER
#include "simulate_fluorometer.h"
#endif

#ifdef SCIENCE_SIMULATE_WIND
#include "simulate_wind.h"
#endif

#ifdef SCIENCE_SIMULATE_CURRENT
#include "simulate_current.h"
#endif

#ifdef SCIENCE_SIMULATE_AIRPRESSURE
#include "simulate_airpressure.h"
#endif

#ifdef SCIENCE_USE_GOTEMP_AIRTEMP
#include "gotemp.h"
#else
#ifdef SCIENCE_SIMULATE_AIRTEMP
#include "simulate_airtemp.h"
#endif
#endif

#ifdef SCIENCE_SIMULATE_WATERTEMP
#include "simulate_watertemp.h"
#endif

#ifdef SCIENCE_SIMULATE_HUMIDITY
#include "simulate_humidity.h"
#endif

//#define NEED_INSTNAMES
#include "science_main.h"
//#undef NEED_INSTNAMES

#include "../simulator.h"

#define MAXPSU	0.
#define MINPSU 0.
#define MAXDEPTH 3.0
#define MINDEPTH 1.0
#define DEPTHINC 0.1


//#define AIRTEMPDEVICE "/dev/gotemp_0"
#define AIRTEMPDEVICE "/dev/ldusb0"

/**
***************************************************************************
*
* ScienceMain_Init()
*/
/** 
*
* @brief
*
*
**************************************************************************/
static void ScienceMain_Init()
{

	#ifdef SCIENCE_SIMULATE_FLUOROMETER
	Fluorometer_Init();
	#endif
	
	return;
}


/**
***************************************************************************
*
* simulate_Null()
*/
/** 
*
* @brief
*
*
**************************************************************************/
static SCI_DBL simulate_Null(
			POSITION p
			,time_t t)
{
	struct tm *timeinfo;
	
	SCI_DBL sd;	

	sd = status_InitSciDbl();

	sd.dValue = 0.;	

	strncpy(sd.sUnits,"NULL", UNITSTRLEN);

	sd.position = p;
	
	timeinfo = localtime(&t);
//	strftime(sd.sTime, sizeof(sd.sTime), TIMEFORMAT, timeinfo);

	status_SetTimeString(sd.sTime, sizeof(sd.sTime),timeinfo);

	return(sd);
}


/**
***************************************************************************
*
* science_main()
*/
/** 
*
* @brief
* Returns SCI_DATA struct for a single instrument
*  at a given lat,lon
*
*  Parameters:
*    - instr - instrument number
*    - p - POSITION struct
*
*  Returns:
*    - SCI_DBL struct
*
**************************************************************************/
SCI_DBL science_main(
			int instr
			,POSITION p)
{
	SCI_DBL data;
	time_t t;
	static int memory=0;

	#ifdef DEBUG3
	char temp[LOGSTRLEN];
	#endif

	if(memory==0){
		memory=1;
		ScienceMain_Init();
	}


//	data = status_InitSciDbl();
//	data.position = p;
	
	t = time(NULL);

	switch((INSTNUM)instr){
		case UNKNOWN:
		case BATHYMETRY:
		case SALINITY:
			data = simulate_Null(p,t);
			break;
	
		case FLUORESCENCE:
			#ifdef SCIENCE_SIMULATE_FLUOROMETER
			data = simulate_Fluorometer(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;
	
		case AIRPRESSURE:
			#ifdef SCIENCE_SIMULATE_AIRPRESSURE
			data = simulate_AirPressure(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case AIRTEMP:
//			printf("airtemp\n");
			#ifdef SCIENCE_USE_GOTEMP_AIRTEMP
			data = gotemp_GetSciDbl(AIRTEMPDEVICE,p,t);
			#else
			#ifdef SCIENCE_SIMULATE_AIRTEMP
			data = simulate_AirTemp(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			#endif
			break;
			
		case HUMIDITY:
			#ifdef SCIENCE_SIMULATE_HUMIDITY
			data = simulate_Humidity(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case WATERTEMP:
			#ifdef SCIENCE_SIMULATE_WATERTEMP
			data = simulate_WaterTemp(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case WINDSPEED:
			#ifdef SCIENCE_SIMULATE_WIND
			data = simulate_WindSpeed(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case CURRENTSPEED:
			#ifdef SCIENCE_SIMULATE_CURRENT
			data = simulate_CurrentSpeed(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case WINDANGLE:
			#ifdef SCIENCE_SIMULATE_WIND
			data = simulate_WindAngle(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

		case CURRENTANGLE:
			#ifdef SCIENCE_SIMULATE_CURRENT
			data = simulate_CurrentAngle(p,t);
			#else
			data = simulate_Null(p,t);
			#endif
			break;

	}

	#ifdef DEBUG3
	snprintf(temp,LOGSTRLEN,"simulating instrument #%d (%15s)"
			"   at location (%lf,%lf)\n"
			": %f"
			,instr, instnames[instr]
			,p.dLatDeg, p.dLonDeg
			,data.dValue);	

	logfile_LogPrint(temp);
	#endif

	return(data);
}


